package com.dek.ioc.utils;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;

/***
 * 
 *	内省
 *	  基于反射技术操作bean
 */
public class BeanUtils {
	
	/**
	 * 
	 * @param o
	 * @param name
	 * @return
	 */
	public static Method getSetMethod(Object o, String name) {
		try {
			BeanInfo beanInfo = Introspector.getBeanInfo(o.getClass());
			PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
			if (propertyDescriptors != null) {
				for(PropertyDescriptor pd : propertyDescriptors) {
					if (name.equals(pd.getName())) {
						return pd.getWriteMethod();
					}
				}
			}
		} catch (IntrospectionException e) {
			e.printStackTrace();
		}
		throw new RuntimeException("未找到" + name +"的set方法");
	}

}
